<?php 
class BlogAction extends BaseAction{
	public function _initialize(){
		parent::_initialize();
		vendor('libs.SamLib.sampeng');
		$this->assign('mid',(string)$this->mid);
		$this->assign('uid',intval($_GET['uid']));
		
		$status = $this->createFactory('BlogApply')->checkUserApply($_GET['uid']);
		if(!isset($status))	$this->error('该用户未申请开通博客');
		if($status === "0") $this->error('该用户的博客正处于申请期中');
	}

	public function createFactory($name){
		return D($name,'blog');
	}
	
	public function blogList(){
		if(isset($_GET['uid'])){
			$uid = intval($_GET['uid']);
			$map['uid'] = $uid;
		}else{
			$map['uid'] = $this->mid;
			$uid = $this->mid;
		}

		if(isset($_GET['category'])){
			$cate = intval($_GET['category']);
			$map['category'] = $cate;
		}
		if(isset($_GET['date'])){
			$map['cTime'] = array('BETWEEN',$this->getData( str_replace('-','',$_GET['date']) ));
		}
		if(isset($_GET['tagId'])){
			$map['blogId'] = array('in',$this->createFactory('BlogTags')->getBlogIdByTag(intval($_GET['tagId'])));
		}
		$map['status'] = array('in',array(1,2));
		
		
		$field = 'title,blogId,commentCount,readCount,cTime,cover,category,content,uid';
		$result = $this->createFactory('BlogInfo')->getBlogList($map,$field,$order,20);
		$data['uid'] = $uid;
		
		$admin = model('UserGroup')->isAdmin($this->mid);
		$this->setTitle(getUserName($_GET['uid']).'的博客 - 空间');
		
		$this->assign('admin',$admin);
		$this->setRight($data);
		$this->assign($result);
		
		$this->display();
	}
	
 	private function setRight($result){
		$showImageArray = array('detail');
		
		//判断是否是自己
		$my = $this->mid == $result['uid'];
		$category = $this->createFactory('BlogCategory')->getUserCategory($result['uid']);
		//获取前10个标签
		$tags = $this->createFactory('BlogTags')->getTags(10,'cTime',$result['uid']);
		$tags = implode(',',$tags);
		$tagsName = model('Xtag')->getTagName($tags);
		//博客统计
		//$sum = $this->createFactory('BlogInfo')->getBlogSum($this->mid,ACTION_NAME);
		
		//归档
        $date = date( 'Ym',time() );
        $condition['uid'] = $result['uid'];
        $condition['status'] = array('in',array(1,2));
        if(isset($_GET['category'])) $condition['category'] = $_GET['category'];
        if(isset($_GET['tagId'])) $condition['blogId'] = array('in',$this->createFactory('BlogTags')->getBlogIdByTag(intval($_GET['tagId'])));
        $date = self::paramData( $date,6,'ts_blog',$condition);

        $data['date'] = $date;
        $data['sumer']  = $this->createFactory('BlogInfo')->getUserBlogCount($result['uid']);
        
        
        
        
        //处理归档链接
		$url = array();
		$tagUrl = array();
		
		$curl = APP_NAME.'/'.MODULE_NAME.'/blogList';
		
		$categoryUrl = $this->getRightUrl('category','tagId','date','how','mid');
		$tagUrl      = $this->getRightUrl('tagId','category','date','how','mid');
		$url         = $this->getRightUrl('date','how','mid');
		
	    $tags = getBlogTags($tags,$tagsName,' ',U($curl,$tagUrl));
		
		$this->assign($data);
		$this->assign('right_tags',$tags);
		$this->assign('right_category',$category);
		$this->assign('show',in_array(ACTION_NAME,$showImageArray));
		$this->assign('url',U($curl,$url));
		$this->assign('categoryUrl',U($curl,$categoryUrl));
		//将Tags换成链接
		$this->assign('myAdmin',$my);
		$this->assign($result);
	}
	
	private function getRightUrl(){
		$url = array();
		$data = func_get_args();
		foreach($_GET as $key=>$value){
			if(in_array($key,$data) || $key === "app" || $key === "id") continue;
			$url[] = $key.'='.$value;
		}
		return implode('&',$url);
	}

	
        /**
         * paramData
         * 解析日期
         * @param mixed $date 当前时间（200905格式）
         * @param mixed $object 需要查询数据的object名.
         * @static
         * @access private
         * @return void
         */
        private  function paramData( $date,$limit = 6,$tableName,$condition){
        	$result = D ( 'Blog' )->field('count(1) as count,from_unixtime(`cTime`,\'%Y-%m\') as day')->where($condition)->group('day')->order('day DESC')->findAll();
        	$limit_time = array();
        	foreach ( $result as $value ) {
        		$limit_time [$value ['day']] ['count'] = $value ['count'];
        		$limit_time [$value ['day']] ['content'] = $value['day'];
        	}
        	return $limit_time;
        }
        
        /**
         * getData
         * 处理归档查询的时间格式
         * @param string $findTime 200903这样格式的参数
         * @static
         * @access protected
         * @return void
         */
        private function getData( $findTime ){
            //处理年份
            $year = $findTime[0].$findTime[1].$findTime[2].$findTime[3];
            //处理月份
            $month_temp = explode( $year,$findTime);
            $month = $month_temp[1];
            //归档查询
            if ( !empty( $month ) ){

                //判断时间.处理结束日期
                switch (true) {
                    case ( in_array( $month,array( 1,3,5,7,8,10,12 ) ) ):
                        $day = 31;
                        break;
                    case ( 2 == $month ):
                        if( 0 != $year % 4 ){
                            $day = 28;
                        }else{
                            $day = 29;
                        }
                        break;
                    default:
                        $day = 30;
                        break;
                }
                //被查询区段开始时期的时间戳
                $start = mktime( 0, 0, 0 ,$month,1,$year  );

                //被查询区段的结束时期时间戳
                $end   = mktime( 24, 0, 0 ,$month,$day,$year  );

                //反之,某一年的归档
            }elseif( isset( $year[4] ) ){
                $start = mktime( 0, 0, 0, 1, 1, $year );
                $end = mktime( 24, 0, 0, 12,31, $year  );
            }else{
                //其他操作
            }

            return array( $start,$end );

        }
}
?>